package com.example.administrator.testproject.download.util;

import android.database.sqlite.SQLiteDatabase;

import com.example.administrator.testproject.downM3U8.LiteM3U8DownInfo;
import com.example.administrator.testproject.download.bean.DownInfo;
import com.example.administrator.testproject.download.bean.LiteOrmDownInfo;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dizner on 2017/12/7.
 * 数据库工具
 */

public class DBUtils extends BaseUtils {
    private static final String dbName = "edu_student.db";

    private static LiteOrm liteOrm;

    public static LiteOrm getLiteOrm() {
        if (liteOrm == null) {
            liteOrm = LiteOrm.newCascadeInstance(mContext, dbName);
        }
        return liteOrm;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        SQLiteDatabase db = getLiteOrm().getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        SQLiteDatabase db = getLiteOrm().getWritableDatabase();
        return db;
    }

    public static List<LiteOrmDownInfo> queryDownAll() {
        List<LiteOrmDownInfo> qb = getLiteOrm().query(LiteOrmDownInfo.class);
        return qb;
    }
    public static List<LiteM3U8DownInfo> queryDownAll(Class cs) {
        List<LiteM3U8DownInfo> qb = getLiteOrm().query(cs);
        return qb;
    }
    public void save(DownInfo info){
        getLiteOrm().insert(info);
    }

    public void update(DownInfo info){
        getLiteOrm().update(info);
    }

    public void deleteDowninfo(DownInfo info){
        getLiteOrm().delete(info);
    }


    public LiteOrmDownInfo queryDownBy(long Id) {
        //模糊查询
        QueryBuilder<LiteOrmDownInfo> qb =
                new QueryBuilder<LiteOrmDownInfo>(LiteOrmDownInfo.class)
                        .whereEquals("id", Id);

        ArrayList<LiteOrmDownInfo> query = getLiteOrm().query(qb);
        if(query.isEmpty()){
            return null;
        }else{
            return query.get(0);
        }
    }

}
